/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.sys.service.impl

import com.gitee.minimalismstyle.fresh.common.core.const.CacheConst
import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.domain.Resource
import com.gitee.minimalismstyle.fresh.common.core.service.ResourceService
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.sys.dao.SysResourceDao
import com.gitee.minimalismstyle.fresh.sys.api.model.SysResource
import com.gitee.minimalismstyle.fresh.sys.api.service.SysResourceService
import com.gitee.minimalismstyle.fresh.sys.dao.SysResourcePermissionDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Primary
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Service

/**
 * 资源业务实现
 * @author maochao
*/
@Primary
@Service
class SysResourceServiceImpl: ResourceService, SysResourceService, LongModelBaseServiceImpl<SysResourceDao, SysResource>(){

    @Autowired
    private lateinit var redisTemplate: StringRedisTemplate
    @Autowired
    private lateinit var sysResourcePermissionDao: SysResourcePermissionDao

    override fun getResourceByServiceId(serviceId: String): R<List<Resource>> {
        val resources = arrayListOf<Resource>()
        val sysResource = dao.getResourceByServiceId(serviceId)
        sysResource.forEach {
            val roles = sysResourcePermissionDao.findRoleByResourceId(it.id!!)
            if(it.link != null){
                resources.add(
                        Resource(
                                link = it.link!!,
                                httpMethod = it.httpMethod,
                                roleList = roles
                        ))
            }

        }
        return R.success(resources)
    }

    override fun findByType(type: String): R<List<SysResource>> {
        val sysResourceList = dao.findByType(type)
        return R.success(sysResourceList)
    }

    override fun findRootResources(): R<List<SysResource>> {
        val sysResourceList = dao.findRootResources()
        return R.success(sysResourceList)
    }

    override fun findByParentId(parentId: String): R<List<SysResource>>{
        val sysResourceList = dao.findByParentId(parentId)
        return R.success(sysResourceList)
    }

    override fun reload(): R<Boolean> {
        redisTemplate.convertAndSend(CacheConst.RESOURCES_RELOAD_TOPIC, "资源权限更新")
        return R.success(true)
    }
}